/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.qx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.qx.entity.QxLeagueMatches;
import com.thinkgem.jeesite.modules.qx.entity.QxTeam;
import com.thinkgem.jeesite.modules.qx.service.QxTeamService;
import com.thinkgem.jeesite.modules.qx.utils.QxCacheUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 单表生成Controller
 * @author zequs
 * @version 2017-08-23
 */
@Controller
@RequestMapping(value = "${adminPath}/qx/qxTeam")
public class QxTeamController extends BaseController {

	@Autowired
	private QxTeamService qxTeamService;
	
	@ModelAttribute
	public QxTeam get(@RequestParam(required=false) String id) {
		QxTeam entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = qxTeamService.get(id);
		}
		if (entity == null){
			entity = new QxTeam();
		}
		return entity;
	}
	
	@RequiresPermissions("qx:qxTeam:view")
	@RequestMapping(value = {"list", ""})
	public String list(QxTeam qxTeam, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<QxTeam> page = qxTeamService.findPage(new Page<QxTeam>(request, response), qxTeam); 
		model.addAttribute("page", page);
		return "modules/qx/qxTeamList";
	}

	@RequiresPermissions("qx:qxTeam:view")
	@RequestMapping(value = "form")
	public String form(QxTeam qxTeam, Model model) {
		model.addAttribute("qxTeam", qxTeam);
		return "modules/qx/qxTeamForm";
	}

	@RequiresPermissions("qx:qxTeam:edit")
	@RequestMapping(value = "save")
	public String save(QxTeam qxTeam, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, qxTeam)){
			return form(qxTeam, model);
		}
		qxTeamService.save(qxTeam);
		QxCacheUtils.putCache("team_key"+qxTeam.getId(), qxTeam);
		QxCacheUtils.putCache("team_name_key"+qxTeam.getName(), qxTeam);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/qx/qxTeam/?repage";
	}
	
	@RequiresPermissions("qx:qxTeam:edit")
	@RequestMapping(value = "delete")
	public String delete(QxTeam qxTeam, RedirectAttributes redirectAttributes) {
		qxTeamService.delete(qxTeam);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/qx/qxTeam/?repage";
	}
	
	/**
	 * 导入用户数据
	 * @param file
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("qx:qxTeam:edit")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/qx/qxTeam/?repage";
		}
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<QxTeam> list = ei.getDataList(QxTeam.class);
			for (QxTeam team : list){
				QxTeam t = QxCacheUtils.geTeamByName(team.getName());
				if (null !=  t) {
					continue;
				}
				boolean flag = false;
				
				QxLeagueMatches matches = QxCacheUtils.getQxLeagueMatchesByName(team.getLeagueName());
				if (null != matches) {
					team.setLeagueid(matches.getId());
					successNum++;
					flag = true;
				}
				if (!flag) {
					logger.error("联赛名错误，联赛名：{}没有找到", team.getLeagueName());
					failureNum++;
					failureMsg.append("联赛名：" + team.getLeagueName() + "导入错误," );
				}
				qxTeamService.save(team);
				QxCacheUtils.putCache("team_key"+team.getId(), team);
				QxCacheUtils.putCache("team_name_key"+team.getName(), team);
			}
			if (failureNum>0){
				failureMsg.append("，失败 "+failureNum+" 条数据，导入信息如下：");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条数据"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入用户失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/qx/qxTeam/?repage";
    }
}